{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import一下\n",
    "import sqlite3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 居然自带，省事了，开心。\n",
    "qie = sqlite3.connect('qieyun.sqlite3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(16494, '會', 2432, '會', '匣合一泰去', 1, 35, '匣', '影', '合', 1, '一', None, '泰', '13', '泰', 4, '蟹', '去', '黃', '外', '黃外切', 'ghuad', 'huay', 'hwajH', 'ʁwɑjɕ˧˩˨', 'ɦǔi', 'hui4', '合也古作會亦州秦屬隴西郡漢分爲金城郡周爲防隋爲鎭武德初平李軌置會州又姓漢有會[相/祤]黃外切又音儈五')\n",
      "(16518, '會', 2434, '儈', '見合一泰去', 15, 29, '見', '見', '合', 1, '一', None, '泰', '13', '泰', 4, '蟹', '去', '古', '外', '古外切', 'kuad', 'kuay', 'kwajH', 'qwɑjɕ˥˩', 'kǔi', 'kui4', '會稽山名又黃外切')\n"
     ]
    }
   ],
   "source": [
    "# 瞎写的居然没报错\n",
    "with qie:\n",
    "    yuns = qie.execute(\"select * from 廣韻字頭全 where 字頭 = '會'\")\n",
    "    results = yuns.fetchall()\n",
    "    print(len(results))\n",
    "    yuns = qie.execute(\"select * from 廣韻字頭全 where 字頭 = '會'\")\n",
    "    for yun in yuns:\n",
    "        print(yun[:])\n",
    "# 這是一個示例，用於接下來查位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['湊']\n"
     ]
    }
   ],
   "source": [
    "import yitizi\n",
    "# yitizi.get('呵')\n",
    "a = yitizi.get('凑')\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3000\n",
      "['的', '是', '不', '我', '一', '有', '大', '在', '人', '了', '中', '到', '資', '要', '以', '可', '這', '個', '你', '會', '好', '為', '上', '來', '學', '就', '交', '也', '用', '能', '如', '時', '文', '說', '沒', '他', '看', '那', '問', '生', '提', '下', '過', '請', '們', '天', '所', '多', '麼', '小', '之', '想', '得', '工', '出', '還', '電', '對', '都', '機', '自', '而', '子', '後', '訊', '家', '站', '心', '只', '去', '知', '國', '很', '台', '成', '信', '同', '何', '章', '道', '發', '地', '法', '無', '然', '但', '當', '於', '嗎', '本', '年', '現', '前', '最', '真', '新', '和', '因', '果', '意']\n"
     ]
    }
   ],
   "source": [
    "字頻 = []\n",
    "with open('characters_traditional_frequencies.csv', 'r',encoding='utf8') as f: # 您瞧這名字多長\n",
    "    lines = f.readlines()[1:3001] # 我覺得三千差不多了，再多字都不認識了還背啥音韻地位\n",
    "    for line in lines:\n",
    "        字頻.append(line.split(',')[0].strip())\n",
    "print(len(字頻))\n",
    "print(字頻[0:100])\n",
    "# 所以第一個奇怪的編碼是什麽我也不知道，怎麽出現的我也不知道\n",
    "# 我只知道把他去掉（操作主義者萬歲！！）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "查無此字 = []\n",
    "# 字頻 = ['會','天']\n",
    "with open('data-chars.js','w',encoding='utf8') as dc:\n",
    "    dc.write('if (!window.zDict) var zDict = {}; zDict[\"chars\"] = {\\n')\n",
    "    with qie:\n",
    "        for 字 in 字頻:\n",
    "            yuns = qie.execute(\"select * from 廣韻字頭全 where 字頭 = '\" + 字.strip() + \"'\")\n",
    "            臨時字頭 = '\"'+字+'\": {\\n'\n",
    "            count = 0\n",
    "            temp_str_list = []\n",
    "            for yun in yuns:\n",
    "                temp_str = ''\n",
    "                temp_str += '\"'+yun[22]+'\": [\\n'\n",
    "                temp_str += '\"'+yun[4]+'\",\\n'\n",
    "                temp_str += '\"'+yun[17]+'攝；'+yun[13]+'韻；'+yun[3]+'小韻\",\\n'\n",
    "                if yun[12] != None:\n",
    "                    temp_str += '\"重紐'+yun[12]+'類\",\\n'\n",
    "#                 temp_str += '\"'+yun[21]+'\",\\n'\n",
    "# 要反切沒啥用，去掉（其實是因爲這裏報錯了，應該是因爲某些字沒有反切\n",
    "                temp_str += '\"白一平：'+yun[24]+'；推導普通話：'+yun[27]+'\",\\n'\n",
    "                temp_str += '\"《廣韻》：'+ yun[28]+'\"\\n]'\n",
    "                temp_str_list.append(temp_str)\n",
    "                count += 1\n",
    "            if count < 1:\n",
    "                字頻.remove(字)\n",
    "                查無此字.append(字)\n",
    "                continue\n",
    "            dc.write(臨時字頭)\n",
    "            temp_str_joined = ',\\n'.join(temp_str_list)\n",
    "            temp_str_joined = temp_str_joined + '\\n},\\n'\n",
    "            dc.write(temp_str_joined)\n",
    "dc.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "36\n",
      "['錄', '啟', '呆', '乘', '滾', '棋', '殼', '哎', '祂', '埔', '樑', '辣', '淘', '颱', '頹', '嚐', '咕', '啤', '齣', '尷', '罕', '鑫', '鬍', '卅', '琉', '疤', '嗶', '晰', '嚕', '袱', '溉', '戳', '鈺', '壟', '囡', '擱']\n"
     ]
    }
   ],
   "source": [
    "print(len(查無此字))\n",
    "print(查無此字)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "37\n",
      "37\n"
     ]
    }
   ],
   "source": [
    "試試異體 = []\n",
    "for 字 in 查無此字:\n",
    "    試試異體 += yitizi.get(字.strip())\n",
    "print(len(試試異體))\n",
    "試試異體 = list(set(試試異體))\n",
    "print(len(試試異體))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('data-chars.js','a+',encoding='utf8') as dc:\n",
    "    with qie:\n",
    "        for 字 in 試試異體:\n",
    "            yuns = qie.execute(\"select * from 廣韻字頭全 where 字頭 = '\" + 字.strip() + \"'\")\n",
    "            臨時字頭 = '\"'+字+'\": {\\n'\n",
    "            count = 0\n",
    "            temp_str_list = []\n",
    "            for yun in yuns:\n",
    "                temp_str = ''\n",
    "                temp_str += '\"'+yun[22]+'\": [\\n'\n",
    "                temp_str += '\"'+yun[4]+'\",\\n'\n",
    "                temp_str += '\"'+yun[17]+'攝；'+yun[13]+'韻；'+yun[3]+'小韻\",\\n'\n",
    "                if yun[12] != None:\n",
    "                    temp_str += '\"重紐'+yun[12]+'類\",\\n'\n",
    "                temp_str += '\"'+yun[21]+'\",\\n'\n",
    "                temp_str += '\"白一平：'+yun[24]+'；推導普通話：'+yun[27]+'\",\\n'\n",
    "                temp_str += '\"《廣韻》：'+ yun[28]+'\"\\n]'\n",
    "                temp_str_list.append(temp_str)\n",
    "                count += 1\n",
    "            if count < 1:\n",
    "                continue\n",
    "            字頻.append(字)\n",
    "            dc.write(臨時字頭)\n",
    "            temp_str_joined = ',\\n'.join(temp_str_list)\n",
    "            temp_str_joined = temp_str_joined + '\\n},\\n'\n",
    "            dc.write(temp_str_joined)\n",
    "dc.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2762\n"
     ]
    }
   ],
   "source": [
    "print(len(字頻))\n",
    "字頻之字符串 = ''.join(字頻)\n",
    "with open('data-freqs.js','w',encoding='utf8') as df:\n",
    "    df.write('if (!window.zDict) var zDict = {}; zDict[\"freqs\"] = \"')\n",
    "    df.write(字頻之字符串)\n",
    "    df.write('\";')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('data-chars.js','a+',encoding='utf8') as dc:\n",
    "    dc.write('};\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "dc_comma = open('data-chars.js','r',encoding='utf8')\n",
    "lines = dc_comma.readlines()\n",
    "dc_comma.close()\n",
    "lines = lines[:-2]\n",
    "dc_no_comma = open('data-chars.js','w',encoding='utf8')\n",
    "dc_no_comma.writelines(lines)\n",
    "dc_no_comma.write('}\\n};')\n",
    "dc_no_comma.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
